package core.helpers;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;

import core.Pilot;

public class ScreenshotHelper
{
	private final Pilot pilot;

	public ScreenshotHelper(Pilot p_pilot)
	{
		pilot = p_pilot;
	}

	public BufferedImage takePageScreenshot(String path) throws IOException
	{
		BufferedImage pageScreenshot = takeScreenshot();
		File screen = new File(path);
		ImageIO.write(pageScreenshot, "png", screen);

		return pageScreenshot;
	}

	public BufferedImage takePageElementScreenshot(String path, WebElement element) throws IOException
	{
		File screen = new File(path);
		BufferedImage pageScreenShot = takeScreenshot();
		BufferedImage elementScreenshot = crop1(pageScreenShot, element);
		ImageIO.write(elementScreenshot, "png", screen);

		return elementScreenshot;
	}

	private BufferedImage takeScreenshot() throws IOException
	{
		File screen = ((TakesScreenshot) pilot.driver().getDriver()).getScreenshotAs(OutputType.FILE);
		BufferedImage img = ImageIO.read(screen);

		return img;
	}

	public BufferedImage takeElementScreenshot(String path, WebElement element) throws IOException
	{
		File screen = new File(path);
		BufferedImage pageScreenShot = takeScreenshot();
		BufferedImage elementScreenshot = crop(pageScreenShot, element);
		ImageIO.write(elementScreenshot, "png", screen);

		return elementScreenshot;

	}

	private static BufferedImage crop1(BufferedImage pageScreenShot, WebElement element)
	{
		Point point = element.getLocation();
		int eleWidth = element.getSize().getWidth();
		int eleHeight = element.getSize().getHeight();

		// setting the aligned rectangle parameters
		Graphics2D graph = pageScreenShot.createGraphics();
		graph.setColor(Color.RED);
		graph.setStroke(new BasicStroke(3));

		graph.drawRect(point.getX() - 5, point.getY() - 5, eleWidth + 10, eleHeight + 10);
		graph.dispose();

		return pageScreenShot;
	}

	private static BufferedImage crop(BufferedImage pageScreenShot, WebElement element)
	{
		Point point = element.getLocation();
		int eleWidth = element.getSize().getWidth();
		int eleHeight = element.getSize().getHeight();

		// setting the aligned rectangle parameters
		Graphics2D graph = pageScreenShot.createGraphics();
		graph.setColor(Color.RED);
		graph.setStroke(new BasicStroke(3));

		int xRedRectangleMove = 15;
		int yRedRectangleMove = 15;

		if (point.getX() < 15 || (pageScreenShot.getWidth() - point.getX()) < 15)
		{
			xRedRectangleMove = 10;
		}

		if (point.getY() < 15 || (pageScreenShot.getHeight() - point.getY()) < 15)
		{
			yRedRectangleMove = 10;
		}

		graph.drawRect(point.getX() - xRedRectangleMove, point.getY()
				- yRedRectangleMove, eleWidth + xRedRectangleMove * 2,
				eleHeight + yRedRectangleMove * 2);
		graph.dispose();

		// if it would be out of bound it will be cut properly
		BufferedImage elementScreenshot = pageScreenShot.getSubimage(
				point.getX() - 60, point.getY() - 60, eleWidth + 120,
				eleHeight + 120);

		return elementScreenshot;
	}
}
